import sys
from collections import deque
toks = (tok for tok in sys.stdin.read().split())
N = int(next(toks))
r1 = int(next(toks))-1
c1 = int(next(toks))-1
r2 = int(next(toks))-1
c2 = int(next(toks))-1
grid = [[] for _ in range(N)]
for i in range(N):
row = next(toks)
for j in range(N):
if row[j] == "0":
grid[i].append(False)
else:
grid[i].append(True)
def reachable_cells(r, c):
reachable = []
vis = [[False for _ in range(N)] for _ in range(N)]
bfs_q = deque()
bfs_q.append((r, c))
vis[r][c] = True
while len(bfs_q) > 0:
cur_r, cur_c = bfs_q.popleft()
reachable.append((cur_r, cur_c))
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
new_r = cur_r+dx
new_c = cur_c+dy
if new_r < 0: continue
if new_c < 0: continue
if new_r >= N: continue
if new_c >= N: continue
if vis[new_r][new_c]: continue
if grid[new_r][new_c] == 1: continue
bfs_q.append((new_r, new_c))
vis[new_r][new_c] = True
return reachable
first_reachable = reachable_cells(r1, c1)
second_reachable = reachable_cells(r2, c2)
if (r1, c1) in second_reachable:
print(0)
else:
min_land_tunnel_cost = None
for rs, cs in first_reachable:
for rt, ct in second_reachable:
land_tunnel_cost = (rs-rt)**2 + (cs-ct)**2
if min_land_tunnel_cost == None:
min_land_tunnel_cost = land_tunnel_cost
else:
min_land_tunnel_cost = min(min_land_tunnel_cost, land_tunnel_cost)
print(min_land_tunnel_cost)
//اللَّهُ لَا إِلَٰهَ إِلَّا هُوَ ۚ وَعَلَى اللَّهِ فَلْيَتَوَكَّلِ الْمُؤْمِنُونَ
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define Abdulrhem ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
const long long N=2e5+10,mod=1e9+7;
#define all(x) x.begin(),x.end()
#define take(arr,n) for (int i=0;i<n;i++) cin>>arr[i];
#define f first
#define s second
vector<ll>theprimes;
int n,m;
bool vis[55][55];
int dist[1005][1005];
vector<bool>primes(N,1);
void sieve()
{
//o(nlogn)
// memset(primes,1,sizeof primes);
primes[0]=primes[1]=0;
for(int i=2;i*i<N;i++)
{
if(primes[i])
{
//theprimes.push_back(i);
for(int j=i*2;j<N;j+=i)
primes[j]=0;
}
}
}
pair<int,vector<int>> comp[N];
void modified_seive()
{
for(int i=0;i<N;i++)
{
comp[i].first=i;
comp[i].second.push_back(i);
}
for(int i=2;i*i<N;i++)
{
if(comp[i].first==i)
{
for(int j=i*2;j<N;j+=i) {
comp[j].second.push_back(i);
if (comp[j].first == j)
comp[j].first = i;
}
}
}
}
long long gcd(long long a,long long b)
{
if(b==0)return a;
else{
long long rem=a%b;
return gcd(b,rem);
}
}
ll lcm(ll a,ll b){
return a*b / gcd(a,b);
}
ll mul(int x, int y) {
return 1LL * x * y % mod;
}
ll add(int x, int y) {
return (x + y) % mod;
}
ll power(int x, unsigned int y)
{
int temp;
if (y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
ll binarySearch(ll &maxCoins,ll k,ll m,ll l){
ll left = 1,right = maxCoins;
ll mid;
ll ans = -1;
while(left<=right){
mid = (left+right)>>1;
if ((mid*m)-k >= l){
right = mid -1;
ans = mid;
}
else{
left = mid +1;
}
}
return ans;
}
bool inRange(int i,set<pair<int,int>>ans){
i++;
for (auto it:ans){
if (i>=it.f&&i<=it.s)return 1;
}
return 0;
}
bool allZero (string &b){
for (auto it:b){
if (it == '1')return 0;
}
return 1;
}
bool allOne(string &b){
for (auto it:b){
if (it == '0')return 0;
}
return 1;
}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
char matrix[55][55];
void BFS(int x,int y,set<pair<int,int>>&island){
vis[x][y]=1;
queue<pair<int,int>>q;
q.push({x,y});
while(!q.empty()){
int xx=q.front().f;
int yy = q.front().s;
//cout<<"am in "<<xx<<" "<<yy<<endl;
q.pop();
for (int i=0;i<4;i++){
int xtmp = xx+dx[i];
int ytmp = yy + dy[i];
if (xtmp>=0&&xtmp<n&&ytmp>=0&&ytmp<n){
if (matrix[xtmp][ytmp]=='0'&&!vis[xtmp][ytmp]){
vis[xtmp][ytmp]=1;
q.push({xtmp,ytmp});
}
else if (matrix[xtmp][ytmp]=='1'){
island.insert({xx,yy});
}
}
}
}
}
void solve(){
cin>>n;
int x1,y1,x2,y2;
cin>>x1>>y1;
cin>>x2>>y2;
x1--;
y1--;
x2--;
y2--;
memset(vis,0,sizeof vis);
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
cin>>matrix[i][j];
}
}
//cout<<"shit"<<endl;
set<pair<int,int>>island1;
set<pair<int,int>>island2;
BFS(x1,y1,island1);
if (vis[x2][y2]){
cout<<0<<endl;
return;
}
BFS(x2,y2,island2);
int ans = INT_MAX;
for (auto it:island1){
for (auto itt:island2){
int x1 = it.f;
int y1= it.s;
int x2 = itt.f;
int y2 = itt.s;
int tmp = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
ans = min(ans,tmp);
}
}
cout<<ans<<endl;
}
int main() {
Abdulrhem;
//sieve();
//to erase the first character in string use s.erase(0,1);
//long double pi = 3.141592653589793238462643383279502884;
int t = 1;
//cin>>t;
while (t--)
{
solve();
}
return 0;
}
1654C - Alice and the Cake | 369A - Valera and Plates |
1626A - Equidistant Letters | 977D - Divide by three multiply by two |
1654B - Prefix Removals | 1654A - Maximum Cake Tastiness |
1649A - Game | 139A - Petr and Book |
1612A - Distance | 520A - Pangram |
124A - The number of positions | 1041A - Heist |
901A - Hashing Trees | 1283A - Minutes Before the New Year |
1654D - Potion Brewing Class | 1107B - Digital root |
25A - IQ test | 785A - Anton and Polyhedrons |
1542B - Plus and Multiply | 306A - Candies |
1651C - Fault-tolerant Network | 870A - Search for Pretty Integers |
1174A - Ehab Fails to Be Thanos | 1169A - Circle Metro |
780C - Andryusha and Colored Balloons | 1153A - Serval and Bus |
1487C - Minimum Ties | 1136A - Nastya Is Reading a Book |
1353B - Two Arrays And Swaps | 1490E - Accidental Victory |